package com.x.provider.customer.controller.rpc;

import com.x.core.domain.SuggestionTypeEnum;
import com.x.core.utils.JsonUtil;
import com.x.core.web.api.R;
import com.x.core.web.controller.BaseRpcController;
import com.x.provider.api.customer.model.dto.common.SetEntityAddressReqDTO;
import com.x.provider.api.customer.service.CommonRpcService;
import com.x.provider.api.oss.model.dto.green.ContentGreenResultDTO;
import com.x.provider.customer.model.domain.common.GenericAttribute;
import com.x.provider.customer.service.common.EntityAddressService;
import com.x.provider.customer.service.common.GenericAttributeService;
import com.x.provider.customer.service.customer.CustomerMcService;
import com.x.provider.customer.service.customer.CustomerResumeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rpc/common")
public class CommonRpcController extends BaseRpcController implements CommonRpcService {

    private final GenericAttributeService genericAttributeService;
    private final EntityAddressService entityAddressService;
    private final CustomerMcService customerMcService;


    public CommonRpcController(GenericAttributeService genericAttributeService,
                               EntityAddressService entityAddressService,
                               CustomerMcService customerMcService){
        this.genericAttributeService = genericAttributeService;
        this.entityAddressService = entityAddressService;
        this.customerMcService = customerMcService;
    }

    @PostMapping("/notify/generic/attribute/green/result")
    public R<Void> notifyGenericAttributeGreenResult(@RequestBody ContentGreenResultDTO greenResult){
        GenericAttribute genericAttribute = JsonUtil.parseObject(greenResult.getExtra(), GenericAttribute.class);
        genericAttributeService.onDraftAttributeGreenFinished(genericAttribute, SuggestionTypeEnum.valueOf(greenResult.getSuggestion()));
        customerMcService.onResumeAttributeSave(genericAttribute.getKeyGroup(), genericAttribute.getEntityId(), genericAttribute.getKey(), genericAttribute.getValue());
        return R.ok();
    }

    @PostMapping("/entity/address/set")
    @Override
    public R<Void> saveEntityAddress(@RequestBody SetEntityAddressReqDTO setEntityAddressReqDTO) {
        entityAddressService.setEntityAddress(setEntityAddressReqDTO);
        return R.ok();
    }

}
